﻿using FrameworkManapro.BusinessModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using GMAC.ReportingSystem.Business.Entities;

namespace GMAC.ReportingSystem.DataAccess
{
    public class ProductDataAccess : BaseDataAccess<Business.Entities.Product>
    {
        public override Business.Entities.Product Get(object id)
        {
            var parameters = new ArrayList() { id };
            var dt = DataAccessComponent.ExecuteSelect("procAccountTypeByIdSelect", parameters);
            if (dt != null && dt.Rows.Count > 0)
                return ReadRow(dt.Rows[0]);
            return null;
        }

        protected override GMAC.ReportingSystem.Business.Entities.Product ReadRow(System.Data.DataRow row)
        {
            GMAC.ReportingSystem.Business.Entities.Product Prod = null;
            if (row != null)
            {
                try
                {
                    Prod = new Product() 
                    {
                        AccountTypeID = int.Parse(row["AccountTypeID"].ToString()),
                        ProductName = row["ProductName"].ToString(),
                        Description = row["Description"].ToString(),
                        AccountTypeCode = row["AccountTypeCode"].ToString(),
                    };                                     
                }
                catch 
                {
                    throw;
                }
            }
            return Prod;
        }

        protected override List<GMAC.ReportingSystem.Business.Entities.Product> ReadTable(System.Data.DataTable table)
        {
            Business.Entities.ProductCollection Products = new GMAC.ReportingSystem.Business.Entities.ProductCollection();
            if (table != null && table.Rows.Count > 0)
            {
                try
                {
                    Business.Entities.Product Product;
                    foreach (System.Data.DataRow row in table.Rows)
                    {
                        Product = ReadRow(row);
                        if (Product != null)
                        {
                            Products.Add(Product);
                        }
                    }
                }
                catch
                {
                    throw;
                }
            }
            return Products;
        }

        internal GMAC.ReportingSystem.Business.Entities.Product ReadProduct(System.Data.DataRow row)
        {
            return ReadRow(row);
        }

        public GMAC.ReportingSystem.Business.Entities.ProductCollection GetList()
        {
            var dt = DataAccessComponent.ExecuteSelect("procAccountTypeBySelect", null);
            return ReadTable(dt) as GMAC.ReportingSystem.Business.Entities.ProductCollection;
        }

        //public GMAC.ReportingSystem.Business.Entities.ProductCollection Search(string ProductName, string ProductCode)
        //{
        //    var parameters = new ArrayList() {ProductName, ProductCode};
        //    System.Data.DataTable dt = DataAccessComponent.ExecuteSelect("procProductByFilterSelect", parameters);
        //    return ReadTable(dt) as GMAC.ReportingSystem.Business.Entities.ProductCollection;
        //}
        //public GMAC.ReportingSystem.Business.Entities.ProductCollection GetbyCode(string code)
        //{
        //    var parameters = new ArrayList() { code };
        //    var dt = DataAccessComponent.ExecuteSelect("procProductBySelectCode", parameters);
        //    return ReadTable(dt) as GMAC.ReportingSystem.Business.Entities.ProductCollection;
        //}

        //public GMAC.ReportingSystem.Business.Entities.ProductCollection GetProductList(int bankId, int extractOutPut)
        //{
        //    ArrayList parameters = new ArrayList() { bankId, extractOutPut };
        //    DataTable dt = DataAccessComponent.ExecuteSelect("procProductList", parameters);
        //    return ReadTable(dt) as GMAC.ReportingSystem.Business.Entities.ProductCollection;
        //}
    }
}
